Cleanup, added more comments, changed the names of confusing and/or inconsistent variables, etc.
Fixed a compilation error.