move waiting stuff into separate function