fixed the intersection bug, with a helper function added