refactor use separate lock instead of having setTerm function