Changed index var in loops from $ to $index