[Refactor]: merge copy-pasta code into addInvariant()

Copy-pasta is bad style. No functional changes.