refactoring swap handling