Fix truncation bug in encoder