refactor text extraction and fix test