refactor and foreach