replace more TreeMap with FlatMap