Added asynchronous locking to prevent a race condition.