added error handling and logs