refractoring...
some fixes
mod_inv changes