Big fix for smith normal form