Various small optimizations and fixes.