refactored graph-related code