- template code cleanup and various fixes