lib: reorganize code