sort refactored