refactoring train-test code