wrote a big part of the merge-join function, but it needs to be changed in some parts