adding error handling and comments