Moved code around
Added detection of document() use in TemplateChecker