Updated python implementation to match c.
Simplify decoding.