better handling of invalid characters in strings