Refactor string functions, fix many defects.

It's not though, since it's already widely used in the code :(.