Fix performance and correctness of finding the <head> and </head> tags