Ported C code to Python 3 syntax