Refactor domain query

@nickpell 

Will probably be some merge conflicts with your work. Feel free to merge yours first.