Various buildscript cleanup.

There's a lot of changes here, but I tried to keep them small and incremental.

Let me know if there's anything you're unhappy with and I'll recraft this.