Improve encoding support

Ref #1078