fix database io error & refactoring code