Make code thread safe even on antique/broken compilers. Improve examples and add more tests