improvement union and intersect function