Py3K compatibility

Various fixes to allow running on Python 3 as well as Python 2.