Some further refactoring and test corrections
Fix problem with gtest and pthread on some systems