Refactored from the training code