lib: rearrange code