finished swap function