refactor to allow custom queries, add more views and test
improve median query