compared with David's code. Discrepancy when NE < NI