Bug fixes

Rollup of some bug fixes and new features.