This is a lot of code that applies to a particular class of store, and it's followed by another check against another kind of store.  Can't we put this into a method on ModuleStore, and the classes can implement it differently?