fixed duplicate locations and added IO error handling if write fails