Add regression test for build order bug
Use recursive algorithm for build order