Replaced FOR loops with equivalent command using EACH or EACH_WITH_INDEX syntax