Made queries more robust.

If values are not defined, don't even include them in the query.