Converting code to work on both python2.7 and python 3