Rework atomic_writing with tests & docstring