rationalize code in do_matprod, fix bug