Improved error handling and JSON streaming