fixing bug in wait loop