refactoring to reduce code duplication