A few more optimizations and fixes