Fix data race issues by locking everything