Refactor string functions, fix many defects.

Not required.