Atomic write to fix error