Python 3 compatibility - Second pass

Passes all the tests from tests.py in both Python 2 and 3