This logic is also shared by `reject` so it makes sense to put it in one place.