Add onError
Add debugging code