made error event handling better