Handle duplicate module names more gracefully

We now give a specific message about duplicate module names, and we look through modules in breadth-first order to try and ensure the "most important" module with the same name is used.