A better way to control exactly the number of loops.