some refactoring and beginning of work for the training algorithm