refactor of loops