Moves output to templates, fixes undefined notices