Refactoring master template loading

This is a simple version of what I have in mind. I'll improve it later.