refine train's code