factored out as lib