refactored template methods to remove duplication and increase testability