Improve the position joining and swapping code