refactored duplicated code