Fix final encoding bugs (thanks @wmeissner!)