refactor get_charset code