Refactored code and clean up