Completed the first version that treats the whole training set.