refactoring and new template functionality