Fix several database related issues