Replaced sleep with wait