Template fix and more refactoring