Optimize kernel. Some cleanup