Some important bug fixes and performance optimizations