move timeout test out of while loop