small refactor of loops