replace loop with fold; use vector of pairs.