def merge_close(events, min_interval, merge_to_longer=False):
    half_iv = min_interval / 2
    merged = []
    for higher in events:
        if not merged:
            merged.append(higher)
        else:
            lower = merged[-1]
            if higher['start'] - half_iv <= lower['end'] + half_iv:
                if merge_to_longer and (higher['end'] - higher['start'] >
                lower['end'] - lower['start']):
                    start = min(lower['start'], higher['start'])
                    higher.update({'start': start})
                    merged[-1] = higher
                else:
                    end = max(lower['end'], higher['end'])
                    merged[-1].update({'end': end})
            else:
                merged.append(higher)
    return merged