Corrected the training function