Fix logic bugs in dependency resolution

Recursive binary dependency resolution was not actually working. Also,
the part where we limit our dependencies to only packages we intend to
build is moved to the very end of the process, because otherwise we were
unable to build in case when we build a package without its dependency.