Some bug fixes and some performance improvements