refactor with Matrix class