Added general logic for error handling