Working version of encoder