refactoring,improve error handling,add database deletion