Re-did the graph traversal in build

Now it first topologically orderes the nodes, so no recursive checking
for recompilation is needed.