Major update. Code now uses NetworkX