Broadened 'except' clause to prevent error handler from crashing

I also refactored an obsolete function definition into inline code.