Fixed bug in new wait function