change the function simplifyGraph, make it more comprehensive.