Refactored code into method