Fix race condition causing crash