Refactoring galore

This is actually a sub-optimal refactoring, because filter and query can be placed inside internal, thus simplifying api.h.

However, the regression caused is crazy if I do that.
I thought no one should care about the type of the exception? Which is why we use the same base classes in utils, which will be caught by GUI regardless of the type.