added new logic to determine idx2