Modifies loop to avoid ruby segmentation fault