Refactor joining queries to separate module