refactored swap method