Added networkx support. Refactored.