Several bug fixes and tweaks.