Refactor string functions, fix many defects.

Yeah, it would be nice to clean it up.