def merge(lsts):
    """
    This function takes a list of lists and merges any subsets that have 
    overlapping elements.  Written by Adam Lowrance.
    """
    out = []
    while len(lsts) > 0:
        # splits lsts into the first element and the rest
        first, *rest = lsts
        # turns first into a set
        first = set(first)
        # Make sure we enter the while loop
        lf = -1
        while len(first) > lf:
            # this ensures we stay in the while loop as long as first gets longer
            lf = len(first)

            rest2 = []
            for r in rest:
                # check to see if there is an element in common between first and r
                if len(first.intersection(set(r))) > 0:
                    # if there is an element in common, take the union of first and r
                    first |= set(r)
                else:
                    # if there is no element in common, save r for later use
                    rest2.append(r)
            # After we've saved all the different r's that don't intersect first, rename them as rest
            rest = rest2
        # We've found everything that intersects with first, so save it
        out.append(first)
        lsts = rest
    return out