refactored and changed to python 3