Tidied up graph code