refactoring: join 2 indentifical loops into one