rewrite graph class based on this example: http://stackoverflow.com/a/9544595/1804624