optimize (speed) duplicate folder test and cleanup code