simplify and remove temporaries
use special wait function