revised code to accommodate solutions of matrices