Performance fixes

Various performance improvements.