It’s a bit unwieldy in places, but should improve the safety of the code in the face of possible race conditions.