refactored the database calls and added more tests