added error handling and callback null check