Updated for matrix stacks.