Refactored duplicated code