write to a temporary file and rename it later

this is fixed now