Cleaned up and refactored code