Refactored and cleaned up some code.