Encoder/decoder algorithm