Fix some database queries