The train function