updated encode.c with memory block copying