refactor, use swift functions equal() and swap()