Optimize overlaps() function.