added swap check to swap function