Change functions to work for matrix