fixed a potential race condition