Refactored repeated code into method