added working encoder code