Fixing a few database related bugs