def generate_authors_and_institutes(mag_authors, good_lookup, grid_lookup):
    """Use MAG author information to extract institutes. If MAG author information
    isn't available, fall back on the arXiv metadata.
    
    Args:
        mag_authors (list of dict): MAG author information
        good_lookup (dict): GRID institutes with good matches to the arXiv metadata.
        grid_lookup (dict): All institutes in GRID
    Returns:
        (authors, institutes) (list, list): Author and institute names.
    """
    authors, institutes = None, []
    if mag_authors is not None:
        if all('author_order' in a for a in mag_authors):
            mag_authors = sorted(mag_authors, key=lambda a: a['author_order'])
        gids = [author.get('affiliation_grid_id') for author in mag_authors]
        authors = [author['author_name'].title() for author in mag_authors]
        institutes = [grid_lookup[g].name.title() for g in gids if g in grid_lookup]
    if institutes == []:
        institutes = [inst.name.title() for inst in good_lookup.values()]
    return authors, institutes