Make file operations more robust