various optimizations and cleanups