implement encode and decode