rewriting CV code from scratch
vectorised M cross-validation function added