Refactor code with template method