python3 compatibility

Bugfix for the same problem as in #17, but with a more general solution. The script should be now compatible with python3.