Abstract the module detection code into a reusable function