added new logic to determine idx