Optimized and refactored list2text, text2list, and replacetext.