Finished coding for dynamic queries