Moved 40 lines of code from matrix controller to matrix model