implement encode method