Added merging function to avoid the use of hadd.