Refactor to use hasinverse function