more subtle optimizations to reduce function calls