Remove in place UTF-8 encoding and add flexible encoder instead

This is slightly slower, but the flexibility is worth it, and the speed difference
is unlikely to be noticable in practice