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

use a central function for building the query

props @davidmosterd