def merge_dictionaries(to_dict, from_dict, depth=3, replace=True):
    """
    Merges both dictionaries, if replace is True, overwriting data in to_dict with from_dict data
    with the same keys.
    Note: Although function does return merged data, actions are inplace and rewrite to_dict.
    :return: merged dictionaries, returns to_dict reference after the merge.
    """
    for key, value in from_dict.items():

        if issubclass(type(value), dict) and key in to_dict and issubclass(type(to_dict[key]), dict) and depth:
            merge_dictionaries(to_dict[key], value, depth - 1, replace=replace)
        elif not replace and key in to_dict:
            continue
        else:
            to_dict[key] = value

    return to_dict