Add support for weighted graphs. Still needs minor tweaks.