[lib] less nesting of functions