Python 3 port (checked on Python 3.3 and PyPy 3.2