Several performance improvements based on profiling.