def merge_chains(main, *components):
    """Merge multiple chains returned by the components"""
    merged = {}
    components = [main] + list(reversed(components))
    for component in components:
        macrochains = component._get_chains()
        for kind, chains in macrochains.items():
            if kind not in merged:
                merged[kind] = []
            for i, chain in enumerate(chains):
                try:
                    merged[kind][i] += chain
                except IndexError:
                    merged[kind].append(chain[:])
    result = {}
    for kind, chains in merged.items():
        result[kind] = []
        for chain in chains:
            result[kind] += chain
    return result
