Refactor string functions, fix many defects.

Done.