Various optimizations

r? @brson