Refactored to replace for loop ith .each_with_index