fix a fatal bug in encoding