Write to temporary file

This avoids file being partly overwritten while still reading. Was big issue on larger files