Added error handling and comments