Some bug fixes and misc tweaks