finised encode function