refactoring to remove code duplication