Modified to be thread safe