Fix encoder bug, add more assertions