Updated to be thread safe