Use ref matrix to increase performance, but maybe I'll just change it into class