Converted the script to python3 while keeping compatibility with python2