Various bug fixes and tweaks