Modified the code so that it also works in Python 2.7.x