Added defmain to run all stats queries, plus supporting functions and tests