refactor foreach