use flat_map instead of map flatten