Clean up the add_repository function

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

Fixes #18.