Use thread safe functions and fix return values

No match.