Implement `swap` function