made stratification function more memory efficient