modified catchematrix.R