Fix potential race condition