Made the recently added database query code more efficient.