Fixed several small bugs that appeared with the introduction of m2m relations.
Replaced models by 'subqueries' in the query engine.
Will soon start overworking the whole thing to make it further stable.