Fix unicode encoding issue and add better unit test

Some unicode encoding logic from merging with upstream was the issue.

@stvstnfrd