#51: pulled out all merging-related code to merge.scala

This actually resulted in merge.scala being about twice as much code as pipeline.scala, but I still think the approach is good.

With some more refactoring, some of the code might move into the case classes, but I suggest we merge this sooner rather than later.