added try block to handle potentially singular matrix