Encode method implementation