performance optimizations and other fixes