modfied train_model