Fixed error in calculating final index of path