refactoring function generational_replacement