Refactored and cleaned up code.