write to a temporary file and rename it laster