optimized the number of factors routine