Refactoring and prepared for training code