Fixing index bug

Index calculation code was wrong.