relay: rearrange code