Merge inv and invert code