Refactor string functions, fix many defects.

You beat me to it. :)