Major refactoring of build graph algorithm into its own file, more functional