Various bug fixes and small tweaks.