Move label extraction to a separate function