Refactor some prehistoric code (build query str)