Lots of template bug fixes.