Optimizations, tweaks and fixes.

Check commits for info.