Various optimizations
Revert "Various optimizations"

This reverts commit dc7f140dafc678c7eed28c7b2b20e682ce5e4faf.
Minor optimizations and fixes