Refactored, and cleaned up the code.