add toMatrix method and move some stuff around