Optimize (for speed) and factorize code related to marking folders