static ProjectInstance[] sortByBuildOrder(ProjectInstance[] instances)
			{
				var rootProjects = instances.ToDictionary(getProjectGuid);

				var ordered = 
					rootProjects.Keys.SortTopologicallyReverse(
						g1 => getDependentProjectGuids(rootProjects[g1]).Where(rootProjects.ContainsKey));

				return ordered.Select(g => rootProjects[g]).ToArray();
			}
		static ProjectInstance[] projectInstancesOfPaths(ProjectInstance[] allInstances, IEnumerable<string> paths)
		{
			var dictByPath = allInstances.ToDictionary(instance => instance.FullPath.ToLowerInvariant());
			return paths.Select(path => dictByPath[path.ToLowerInvariant()]).ToArray();
		}
		static Dictionary<Guid, HashSet<Guid>> createDependentMap(ProjectInstance[] allInstances)
		{
			var allGuids = allInstances.ToDictionary(getProjectGuid);
			var dict = new Dictionary<Guid, HashSet<Guid>>();
			foreach (var inst in allInstances)
			{
				var guid = getProjectGuid(inst);
				var deps = getDependentProjectGuids(inst).Where(allGuids.ContainsKey).ToArray();
				foreach (var dep in deps)
				{
					HashSet<Guid> dependents = null;
					if (!dict.TryGetValue(dep, out dependents))
					{
						dependents = new HashSet<Guid>();
						dict.Add(dep, dependents);
					}
					dependents.Add(guid);
				}
			}
			return dict;
		}
		static Dictionary<Guid, Guid[]> createDependencyMap(ProjectInstance[] allInstances)
		{
			var allGuids = allInstances.ToDictionary(getProjectGuid);
			var dict = new Dictionary<Guid, Guid[]>();
			foreach (var inst in allInstances)
			{
				var guid = getProjectGuid(inst);
				var deps = getDependentProjectGuids(inst).Where(allGuids.ContainsKey).ToArray();
				dict.Add(guid, deps);
			}
			return dict;
		}
		/// Returns all the affected projects of the given list of projects. 
		/// Since the roots may refer to each other, the roots are included in the result set.
		static ProjectInstance[] getAffectedProjects(ProjectInstance[] allInstances, ProjectInstance[] roots)
		{
			var dependentMap = createDependentMap(allInstances);
			var allGuids = allInstances.ToDictionary(getProjectGuid);
			var rootGuids = roots.Select(getProjectGuid).ToArray();
			var todo = new Queue<Guid>(rootGuids);
			var affected = new HashSet<Guid>(rootGuids);

			while (todo.Count != 0)
			{
				var next = todo.Dequeue();

				HashSet<Guid> dependents = null;
				if (!dependentMap.TryGetValue(next, out dependents))
					continue;

				dependents.ForEach(dep => {
					if (affected.Add(dep))
						todo.Enqueue(dep); }
				);
			}

			return affected.Select(g => allGuids[g]).ToArray();
		}
		/// Returns all the dependencies of a number of projects.
		static ProjectInstance[] getDependencies(ProjectInstance[] allInstances, ProjectInstance[] roots)
		{
			var allGuids = allInstances.ToDictionary(getProjectGuid);
			var todo = new Queue<Guid>(roots.Select(getProjectGuid));
			var rootSet = new HashSet<Guid>(roots.Select(getProjectGuid));
			var dependencies = new HashSet<Guid>();
			while (todo.Count != 0)
			{
				var next = todo.Dequeue();

				getDependentProjectGuids(allGuids[next])
					.Where(g => !dependencies.Contains(g) && !rootSet.Contains(g) && allGuids.ContainsKey(g))
					.ForEach(g =>
					{
						todo.Enqueue(g);
						dependencies.Add(g);
					});
			}

			return dependencies.Select(g => allGuids[g]).ToArray();
		}