misc common functionality and useful optimizations
