Rewrote swap alg