miscellaneous style and logic improvements

This pull request includes a series of incremental (and largely independent) commits to update the codebase to modern style and purpose.  I believe the function to be unchanged, though I haven't tested it extensively. I hope to make more pull requests based on these changes, so please consider including these. Thanks.