Use flat_map instead of map.flatten