Refactor server query system