Optimizations cherry-picked onto master