refactor. moved the element swapping code into a method