Refactor the encoder code on top of the fixes to match the current implementation of the decoder.
WIP