Restructured.

Give this a whirl. Kind of hard to test since the original concern was race conditions, but it seems to make more sense this way.