working on train function