Refactor graph reduction script