Add more testing and refactor some of the db functions