Performance improvement for identify_gaps