Various bugfixes, improvements, style tweaks

This pull request addresses #86, #53, #48, #87, #118, and includes some other minor enhancements for which there weren't issues.