Optimizations and fixes.