implemented element swapping in one place only