rearrange algorithm, fixed the 'last curve not correct' bug