Fixed some database bugs