Refactor rearrange array