Refactored mod2 code