Fix idx calculation