refactored duplicated code.