Working refactored code