modified to add condtional query