Reworked SPARQL part for simplification and to prevent memory leaks.

Change-Id: I49b9abb5415073b1d8f156ebd5e13b11ccabdccd