py3 port, manualy checked after 2to3
tests passes for py2