add in new codes into Encode function