Speedups and refactoring.