refactoring to extract duplicated code