fully working version with genetic algorithm