Build fixes

Another batch of build fixes with more refactoring and some convenience targets. This is another step towards making the build system more declarative, i.e. more resembling a Makefile rather than a large shell script.