More optimizations and fixes