refactoring of the sort-related code + insertion sort