Made the templates compatible with entity hierarchies and did some template code cleanup