fix bug concerning race condition