Added better comments and cleaned up code.