Code quality

Flake8 runs as part of Travis. Lots of code cleanup changes but no functional changes in this pull request.

Fixes #19.