Improved performance
Reimplemented rearrange