Updated python implementation to match c.