add error handling and comments