move block of code inside error handler