Refactored code and did some code cleanup.