Simplified code. Making it thread-safe.