flatMaps are nested in for-comps
pattern matching and small improvements elsewhere