Cleaned up and refactorized code.