Ported to python 3.x