refactor product loop, removing some duplicated lines