Improved Graph building algorithm. Need to test.