generic 'swap' function