Rewrote python implementation to abide by the spec.