Perf and cleanup

@jbdeboer I would appreciate if you can review this.

I'm not sure if all the perf changes are ok.

Regarding the style changes, feel free not to merge them or to indicate if some things should be reverted.