Refactored some code in invariant.py, nothing changed removed redundancy