Replace weird poll() loop with a wait() call