Sorting refined and input bug fixed