refactor using generator and select