Refactored code after feedback