Refactor string functions, fix many defects.

Same here