organizing the detection code in a more sensible way: