various optimizations, refactors