Refactors join queries.