refactors nextGeneration method to speed up algorithm by removing nested loops