final refactored code