write to a temporary file and rename it later