Refactored long merge function.