replace sleep by wait