Added some functions for actually exporting data - featuring some of the most horrific SQL you've ever seen.