Cleaned and refactored