Updated and refactored code