refactored swap_with_next from 4 loops to 1