function [model] = kfoldCV(classification,trainingData,k,cExpMax)

    randMap=randperm(size(trainingData,1));
    leaveOut=trainingData(mod(randMap,k+1)==0,:,:);
    leaveClasses=classification(mod(randMap,k+1)==0);
    remaining=trainingData(mod(randMap,k+1)~=0,:,:);
    remainingClasses=classification(mod(randMap,k+1)~=0);
    cvAccurancy=zeros(2*cExpMax+1,1);
    for cExp=-cExpMax:1:cExpMax
        c=2^cExp;

        randomMapping=randperm(size(remaining,1));
        accurancy=zeros(k,3);

        parfor i=1:k
            trainData=remaining(mod(randomMapping,k)+1~=i,:,:);
            testData=remaining(mod(randomMapping,k)+1==i,:,:);
            trainClasses=remainingClasses(mod(randomMapping,k)+1~=i);
            testClasses=remainingClasses(mod(randomMapping,k)+1==i);
            [trainClasses,trainData]=balanceClasses(trainClasses,trainData,0.00001);
            model=svmtrain(trainClasses,trainData(:,:),sprintf('-t 0 -c %f -q',c));
            [~, accurancy(i,:), ~]=svmpredict(testClasses, testData(:,:), model,'-q');
        end

        cvAccurancy=mean(accurancy(:,1));
    end
    [~,i]=max(cvAccurancy);
    bestC=2^(i-cExpMax-1);
    [balanceLeftClasses, balanceLeftOut]=balanceClasses(leaveClasses,leaveOut,0.00001);
    model=svmtrain(balanceLeftClasses, balanceLeftOut(:,:),sprintf('-t 0 -c %f -q',bestC));

end