reorganised the detection routine slightly