Tidied up sort_r