Handle case where local data file doesn't exist
Potential fd leak
delete dead code
Write data file atomically