refacorted to work both in Python 2 and 3