More cleanup and optimization