Bug fixes.

For @jmathai to review. To merge to master.

See individual commits for bug fixes.