Refactored code into smaller methods