Added most of the documentation; assorted code cleanups