Ported the code to run on Python 2 and 3