Python 3 compatibility

The tests pass. This also closes #22.