added encoder code. not sure if it works