basic implementation of encode and decode