fix $index that was not incrementing