Attempt at fixing database query bug