Refactored database session stuff to be cleaner.