added a listener event to the OnERROR event, and put all logic for redirect handler into a reusable method, fixes #3, fixes #2