added code to build and write graphs