Make changeIndex a much more general function