error fixing and rewriting the swap function