Optimized version of dmerge