Converted to python 3 and cleaned up