Correct handling of the <template> tag content fragments. Fixes #942
Some minor cleanups