added handling ValueError in to_text() function