Lots of fixes for Python 3.x compatibility