some bugs in the sort code