Master bugfixes

Here's a stack of drive-by bugfixes.  Some are clearly needed.  Others are of questionable correctness (ac55139) or value (4e26342), so please review, and feel free to cherry-pick.  Thanks!