delay encoding until after line change detection

a line cannot be encoded until after the line change detection since
line encoding updates the dictionaries and this would have to be undone
if the line did not change, therefore the encode call was moved to after
the line change check, and an encode call was added to the insert
operation section