Write files atomically

Fixes #127.