fix potential race conditions