plotting codes, some changes to xval routine for robustness