Fixed defects and cleaned up code.