Better hanlding for queries and json responses