Rewrite swap function