refactored merge procedure