viewer: rearrange code