Various tweaks & bug fixes, mostly for encoding.