Optimizations

Optimizations, split off from pull request 738