Piecing out search query to make it more dynamic, adding checks for variable existence in a really clunky way