Rewrote merge operation to be non-recursive and use loops