Updated Encoder function