Optimized the loop through datasets