refactor text processing functions