pretty json return for queries

and other fixes