refactored quad and invquad