Lots and lots of fixes/optimizations.