Refactored for crossvalidation.