Reworked implementation of custom error handler