added some error handling and persistence