Revert "Revert "some optimizations""

This reverts commit 0045c2200444a9c19d6293d9674ce814cc95a7dc.