[WIP] Replace additional instances of map.flatten with flat_map

DO NOT MERGE. This is still a work in progress.