Refactor namespace determination in ModelBuilder

After this commit the code is easier to follow (and faster) - I hope :neckbeard: