Fixed database query bug