More memory efficient merging algorithm.