now also works if the loop tooks longer than it should