Towards Python 2/3 compatibility

Here is an initial set of patches for a Py2/3 compatible codebase. Python 3 is not yet supported, but these initial patches should be easy to review and take us in the right direction.