Make file I/O more robust