Refactor the encoder, still a bug in LZ stuff