fixed wait function to avoid zombies