Moved text extraction to its own function.