grep: refactor grep_objects loop into its own function