Move merge function, fix tests