Cleaned and refactored some code.