used a central function for building the query
Merge pull request #1 from davidmosterd/fix2

used a central function for building the query