Function of the train method