def advanced_update(dict1, dict2, newstuff=True, oldstuff=False, newkeys=[],
    oldkeys=[], mask=None):
    """
    updates dict1 with dict2, but with options about which keys to add/update.
    Default values give a normal update.
    """
    keys2 = list(dict2.keys())
    if not newstuff:
        for key in keys2:
            if key not in dict1.keys() and key not in newkeys:
                dict2.pop(key, None)
    if oldstuff or len(oldkeys) > 0:
        for key in keys2:
            if oldstuff and key in dict1.keys() or key in oldkeys:
                dict2.pop(key, None)
    if mask is not None:
        for key in keys2:
            if mask(key):
                dict2.pop(key)
    dict1.update(dict2)
    return dict1
