Revise algorithm for detecting overlapping ranges