drop intermediate collections after final result is generated (Fixes #339)

The type is getting complex enough that a tuple is confusing. Can you use a case class here?