Performance optimizations and bug fixes