Refactor swap and test_swap