Adding onError handling, fixes #1