Various minor bug fixes and some performance improvements