cleaned and refactored code