some refactoring to avoid code duplication