Fixed race conditions that caused crashes