Added thread locking for extra credit.