updated with error handling and corrected typo