Improve textToHtml function