Refactor on top of my trailing whitespace commit

I offer up this refactor of the code. This is just a single commit on top of my trailing-whitespace pull request.

I'm not sure how well github will deal with this. I certainly can hope it can be applied as a single commit on top of my other pull request (no merge should be needed). Or, you could just accept this pull request and ignore my earlier one, since that earlier commit is in here as well.