Ported to python3 using 2to3