Tried to make a bit of thread safe