Fix implementation for Python3 (including PyPy)