Documentation, documentation, documentation (and comments).

Also some minor renaming and some declaration movement.