Refactoring and beginning to add more db functions