refactor/cleanup of much of the text processing code