optimized the merge loop