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.
if this is internal how is anyone supposed to catch the exception?