Adding some debugging and defensive code to the string functions