fixing bug with rearrange elements fxn