Some fixes to handle singular  matrix