Changed functions to apply to matrices instead of vectors