Refactoring  CV function