Move character encoding logic out to common, try again to improve its handling of...