you could add to first find only, if your files is not modified by other MODs.
and for case #2,
search for first edit ( </head> ) and add it,
and then search for that ( end if, end if ) from there and add code.
you will find them if your theme is not modified by other MODs.