Some refactoring and detection improvements