use matrix inv lemma to avoid numerical issues