Misc fixes.

Here are a few fixes to make this work reliably.