Bug fixes

Various bug fixes.