Improved arranging algorithm, but still having some problems with it.