Added two sorting steps to prevent merging error.