Heavily edited the encoder