add some sort and refactoring