correct swapping function