Refactor to foreach cycle