MergeFunctions: reduce complexity to O(log(N))

Hi Nick,
Thank you for review!
Currently I'm in progress. Hope to present fixes till Friday.
-Stepan.