Completely refactored code