New algorithm for finding gaps