Code tweaked to accept dataframe output from queries.