refactored code after feedback