fixed determineIndex logic