Improve write error handling