Bug fix - improved DB query