A few build system fixes

Hi all,

just a few small fixes for the build system — I could do a few more changes, and make the whole thing non-recursive but I'm not sure if it would be accepted.

HTH,
Diego