Refactoring some of the logic so that it actually makes it into the database