better detection logic