Better implementation of genetic algorithm