Python 3 compatibility

The following set of commits add python3 support without breaking backwards compatibility.