optimizations and minor changes