refactor DB.php and error processing