Some cleanup and optimization