A few fixes and cleanups for the #52 pull request.

This fixes a few bugs in the #52 pull request, and also tidies up a few stylistic things.