Improved generic build_query function - prevented attaching empty terms ...

...to query.