Modified to make subfolders and recursive optimization work.