Reorganise querysort a bit

Also kill superflous function and test. Is this a candidate for its
own file?