refactored sort to have smaller methods