Fix circular dependency and wrong query