Fix sort method and some bugs