added some error handling determination logic
merge with latest version