bugfixes for encode_* functions