Cleaned and refactored code