optimizations and fixes