basic implementation of encode/decode