Finished optimization logic.