refactoring bet sort