Refactored training method.