Working port to Python.

Nearly killed me.