Some packaging improvements, tests, and py3 fixes.

This PR contains the following changes:
* Added a MANIFEST.in file so installing from the git tarball is possible.
* Improved setup.py slightly.
* Refactored some parts of the code and ran everything through flake8.
* Fixed an issue with Python 3.
* Added a test suite and the related boilerplate with support for tox.

Test coverage is 90%, which is better than nothing.