lib: Move subgraph creation into separate function