Some of the code was nasty. I cleaned it up a bit. I also started moving tests into a tests/ directory. You can run python -m unittest to run them. I still need to add more tests to make sure things are working as expected.