Updated encoding/decoding functions