clean & comment code

Signed-off-by: Stéphane Depierrepont <toorop@toorop.fr>
Refactoring error handling

Signed-off-by: Stéphane Depierrepont <toorop@toorop.fr>