Major code update

Also added predicate to check if matrix is valid.