a shorter implementaion of swapPairs