modify function to matrix manipulation