Performance optimizations and other bugfix tweaks.