Base code for detection