Updated to work in both python 2.7 and python3