refactor the function saveAllFiles