Refactor of the swap method