OMP tuning and some other performance fixes. See #877.