A bit of refactoring on sort and shuffle