Rework to make match counting code for searches more DRY and easier to read