Improve Python 3 compatibility

I'm not sure if you have been running this on Python 3, but I ran into some issues that this pull request addresses.  

I used the python2to3 tool and pulled out changes it made that were not necessary.

From what I can tell this doesn't break any python2 compatibility.