Refactored duplicated code.