Refactoring template code