Feature #31: better error handling

Add dedicated exception and exit on specific error code.
Also put resource loading in separated functions.