Port code to python 3