gen loss stop criterion and refactoring