<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def mod_aggregate(low, chunks, running):
    '''
    The mod_aggregate function which looks up all packages in the available
    low chunks and merges them into a single pkgs ref in the present low data
    '''
    pkgs = []
    pkg_type = None
    agg_enabled = [
        'installed',
        'removed',
    ]
    if low.get('fun') not in agg_enabled:
        return low
    for chunk in chunks:
        tag = __utils__['state.gen_tag'](chunk)
        if tag in running:
            # Already ran the pkg state, skip aggregation
            continue
        if chunk.get('state') == 'pip':
            if '__agg__' in chunk:
                continue
            # Check for the same function
            if chunk.get('fun') != low.get('fun'):
                continue
            # Check first if 'sources' was passed so we don't aggregate pkgs
            # and sources together.
            if pkg_type is None:
                pkg_type = 'pkgs'
            if pkg_type == 'pkgs':
                # Pull out the pkg names!
                if 'pkgs' in chunk:
                    pkgs.extend(chunk['pkgs'])
                    chunk['__agg__'] = True
                elif 'name' in chunk:
                    version = chunk.pop('version', None)
                    if version is not None:
                        pkgs.append({chunk['name']: version})
                    else:
                        pkgs.append(chunk['name'])
                    chunk['__agg__'] = True
    if pkg_type is not None and pkgs:
        if pkg_type in low:
            low[pkg_type].extend(pkgs)
        else:
            low[pkg_type] = pkgs
    return low