Speed improvements of queries

There was a huge bottleneck in the API with query times larger than 60 seconds. This is solved.

Additionally there are two small improvements to queries.