made slightly more readable, moving swapping to its own method