cleaned up code, translated to python3