Massive restructuring, removal of debug prints and comments