refactored sparql query generation