Debugged the function merge