Refactored duplicite code.