some more optimizations as suggested by @tractorcow