Clean up the add_repositories function

Raise errors if the repository templates don't exist. And make the function a little bit preatier and easier to understand.

Fixes #18.