Updated the sort functions with corrections from today.
Also merged the upstream script file in so merging back upstream should be easier