Fixed a few database related bugs