Updated code to do fast sorting and merging