Refactor the charset guessing.