factor out finding the gap