simplified the zigzag code in the encoder