Refactor the atom swapping.