Added more robustness to character encoding parsing