Added check for calculating matrix inverse