Some bug and test fixes