Refactored code, WORKING