Optimized and fixed BuildFolderTree