Better error handling and stream ending