Refactoring of code.

Provide a macro for thumbnail generation.
Let the repositories handle the 404 and 401 exceptions.
Change the logic on document creation.