Trying to tighten up database logic