tidied up code for sort.*