Trivial cleanup.
A bit of error checking.
adding comments and stuff, and a little messing around while at it