Improved error handling and charset detection and conversion