Lots of code cleanup and commenting.