use Encode, and some minor code optimization