Replace map.flatten with flat_map