Various fixes motivated by making rebuilding better

Sorry for the whitespace noise in the diffs