refactoring with templates