Generic atomic update code

Couldn't resist doing a little cleanup before writing a test.
At least check and log the error here.