Sprinkle somme comments and factorize error handling.
Make the main function aware of errors.