Refactored backupAndDeleteFile() into rewritef()
Renamed appendf() to writef()