Moved text handling out of C code