Python3 compatibility

The code should work under both Python2 and Python3.