added check for a singular matrix