Optimizations and bug fixes.