Encoding improvements

Follow ups on #245.

CC @akleeman 

(more to come)