Break that optimization conditional out into a utility function.