Modular inverse speedup

Others minor optimizations