fixed error handler, reduced code complexity