def nearest_multiple(from_number, factor, max_val=None):
    """
    Return the nearest (but smaller) multiple of a factor from a given number
    E.g. nearest multiple of 7 from 20 is 14
    :param from_number:
    :param factor:
    :return:
    """
    if max_val and from_number > max_val:
        from_number = max_val

    residual = from_number % factor
    return from_number - residual