so ugly, but the empty matrix problem is resolved