extracted rearrange code into separate function