loads of fixes and optimizations.