Refactor

We've several small code refactoring steps here that make the existing code easier to read and make other features cleaner to add.  In particular, I believe it'll be easier to review these refactoring operations separately from the larger changes they enable.

This supersede the remainder of #136 not covered in #138.  This pull request is based on #138 so it includes those patches until they're merged.