Refactor string functions, fix many defects.

More curly brackets please.