Better handling of encoding and errors