Reformat code and add test cases for DB