Port bits and pieces more for encoding + string logic.

May help #1255.