Fixed output format, add thread safety