             }
         }
         collect.append(overlap)
-        return RangeSet(bounds: collect)
+        return RangeSet(ranges: collect.sorted { $0.lowerBound < $1.lowerBound })
     }
     
     public func subtracting(_ range: Range<Bound>) -> RangeSet {
         var collect: [Range<Bound>] = []
-        for r in self {
+        for r in ranges {
             if range.overlaps(r) {
                 if r.upperBound <= range.upperBound && r.lowerBound < range.lowerBound {
                     collect.append(Range(uncheckedBounds: (r.lowerBound, Swift.min(range.lowerBound, r.upperBound))))


public struct RangeSet<Bound : Comparable> {
    
    public let lowerBounds: [Bound]
    public let upperBounds: [Bound]
    
    public init() {
        self.lowerBounds = []
        self.upperBounds = []
    }
    public init(_ ranges: Range<Bound> ... ) {
        self.init(ranges)

                collect.append(r)
            }
        }
        return RangeSet(ranges: collect.sorted { $0.lowerBound < $1.lowerBound })
    }
    
    public func intersection(_ range: Range<Bound>) -> RangeSet {
        var collect: [Range<Bound>] = []
        for r in ranges where range.overlaps(r) {
            collect.append(r.clamped(to: range))
        }
        return RangeSet(ranges: collect.sorted { $0.lowerBound < $1.lowerBound })
    }
}
