refactoring around template handling