Finished the preliminary code restructuring, cleanup and commenting.