finished encoder code