Refactor string functions, fix many defects.

Okay, that's probably even better.