Some bug fixes and test fixes