fix bug in wait function