Refactored loss detection
AI and I-don't-remember-what fixes