fixing bug with matrix reduction