another potential race condition fix

this one involved me rearranging the logic a bit to make the code
cleaner, so making a separate commit for it