This could probably be more efficient using a known algorithm, and it could definitely require more tests, but is good enough for now.
This further cleans up the pgraph lib to be more generic.
This is the mechanical port of the remaining bits. Next to clean it up a bit.