2nd edit of encoder code