Python implementation up to the same point as the C one.