Added handling of file I/O errors