rewrote swap (untested)